import { useState, useEffect, useRef, useCallback} from 'react';
function useInterval(func, timer) {
  const ref = useRef();
  ref.current = func;
  const clearRef = useRef();
  const clear = useCallback(() => {
    clearRef.current?.()
  }, [])
  useEffect(() => {
    const t = setInterval(() => {
      ref.current()
    }, timer);
    clearRef.current = () => {
      clearInterval(t)
    }
    return clear
  })
  return clear
}

export default function App() {
  const [count, setCount] = useState(0)
  useInterval(() => {
    setCount(count + 1)
  }, 1000)
  // const ref = useRef();
  // ref.current = count
  // useEffect(() => {
  //   const timer = setInterval(() => {
  //     setCount( ref.current + 1)
  //   }, 1000)
  //   return () => {
  //     clearInterval(timer)
  //   }
  // }, [])
  return <>
    <p>{count}</p>
  </>
}